package config;


import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

//用于封装多个property对象
public class MutablePropertyValues implements Iterable<BeanProperty>{

    private final List<BeanProperty> propertyList;

    public MutablePropertyValues(){
        propertyList = new ArrayList<>();
    }

    public MutablePropertyValues(List<BeanProperty> propertyList){
        this.propertyList = propertyList != null ? propertyList : new ArrayList<>();
    }

    //添加BeanProperty
    public MutablePropertyValues addBeanProperty(BeanProperty beanProperty){
        if(beanProperty == null){
            return this;
        }
        //判断该property是否添加过，若添加过则替换
        for(int i = 0;i<propertyList.size();i++){
            BeanProperty property = propertyList.get(i);
            if(property.getName().equals(beanProperty.getName())){
                propertyList.set(i,beanProperty);
                return this;
            }
        }
        propertyList.add(beanProperty);
        return this;
    }

    @Override
    public Iterator<BeanProperty> iterator(){
        return propertyList.iterator();
    }
}
